﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using Eassets.Layered.Domain.Asset;


namespace Eassets.Data.Asset
{
    public class CompanyAssetData
    {
        public void AddCompanyAssets(CompanyAsset companyAsset)
        {
            SqlCommand command = new SqlCommand("SP_InsertCompanyAssets"); // Name of the SP
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, companyAsset));
        }

        /// <summary>
        /// Updates companyAssets.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void ModifyCompanyAssets(CompanyAsset companyAsset)
        {
            SqlCommand command = new SqlCommand("SP_ModifyCompanyAssets"); // Name of the SP
            command.Parameters.AddWithValue("@CompanyAssetsId", companyAsset.CompanyAssetId);
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(ConstructCommand(command, companyAsset));
        }


        /// <summary>
        /// Delete companyAssets.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void DeleteCompanyAssets(long companyAssetsId)
        {
            SqlCommand command = new SqlCommand("SP_DeleteCompanyAssets"); // Name of the SP
            command.Parameters.AddWithValue("@CompanyAssetsId", companyAssetsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); commonData.ExecuteNonQuery(command);
        }

        /// <summary>
        /// GetbyId companyAssets.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public CompanyAsset GetByIdCompanyAssets(long companyAssetsId)
        {
            SqlCommand command = new SqlCommand("SP_GetCompanyAssetsById");
            command.Parameters.AddWithValue("@CompanyAssetsId", companyAssetsId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            CompanyAsset companyAsset = new CompanyAsset();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["CompanyAssetId"].ToString())))
                {
                    companyAsset.CompanyAssetId = Convert.ToInt64(dataReader["CompanyAssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    companyAsset.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    companyAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["IsAssetDepreciable"].ToString())))
                {
                    companyAsset.IsAssetDepreciable = Convert.ToBoolean(dataReader["IsAssetDepreciable"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTime"].ToString())))
                {
                    companyAsset.LifeTime = Convert.ToInt32(dataReader["LifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTimeUnitOfMeasureId"].ToString())))
                {
                    companyAsset.LifeTimeUnitOfMeasureId = Convert.ToInt64(dataReader["LifeTimeUnitOfMeasureId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationMethodId"].ToString())))
                {
                    companyAsset.DepreciationMethodId = Convert.ToInt64(dataReader["DepreciationMethodId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationRate"].ToString())))
                {
                    companyAsset.DepreciationRate = Convert.ToDecimal(dataReader["DepreciationRate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ResidualValue"].ToString())))
                {
                    companyAsset.ResidualValue = Convert.ToDecimal(dataReader["ResidualValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialFormat"].ToString())))
                {
                    companyAsset.AssetSerialFormat = Convert.ToString(dataReader["AssetSerialFormat"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Barcode"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["Barcode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["sys_DateCreation"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["sys_DateCreation"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["sys_DateLastModification"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["sys_DateLastModification"]);
                }
            }
            dataReader.Close();
            return companyAsset;
        }

        /// <summary>
        /// GetAll companyAssets.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public Collection<CompanyAsset> GetAllCompanyAssets()
        {
            Collection<CompanyAsset> companyAssets = new Collection<CompanyAsset>();
            SqlCommand command = new SqlCommand("SP_GetAllCompanyAssetss");
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            CompanyAsset companyAsset;
            while (dataReader.Read())
            {
                companyAsset = new CompanyAsset();
                if (!(String.IsNullOrEmpty(dataReader["CompanyAssetId"].ToString())))
                {
                    companyAsset.CompanyAssetId = Convert.ToInt64(dataReader["CompanyAssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    companyAsset.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    companyAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                    AssetData assetData = new AssetData();
                    companyAsset.Asset = assetData.GetByIdAsset(companyAsset.AssetId);
                }

                if (!(String.IsNullOrEmpty(dataReader["IsAssetDepreciable"].ToString())))
                {
                    companyAsset.IsAssetDepreciable = Convert.ToBoolean(dataReader["IsAssetDepreciable"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTime"].ToString())))
                {
                    companyAsset.LifeTime = Convert.ToInt64(dataReader["LifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTimeUnitOfMeasureId"].ToString())))
                {
                    companyAsset.LifeTimeUnitOfMeasureId = Convert.ToInt32(dataReader["LifeTimeUnitOfMeasureId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationMethodId"].ToString())))
                {
                    companyAsset.DepreciationMethodId = Convert.ToInt32(dataReader["DepreciationMethodId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationRate"].ToString())))
                {
                    companyAsset.DepreciationRate = Convert.ToDecimal(dataReader["DepreciationRate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ResidualValue"].ToString())))
                {
                    companyAsset.ResidualValue = Convert.ToDecimal(dataReader["ResidualValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialFormat"].ToString())))
                {
                    companyAsset.AssetSerialFormat = Convert.ToString(dataReader["AssetSerialFormat"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Barcode"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["Barcode"]);
                }

                companyAssets.Add(companyAsset);
            }
            dataReader.Close();
            return companyAssets;
        }

        public void MapAssetToCompanyAsset(Collection<CompanyAsset> companyAssets)
        {
            Collection<SqlCommand> sqlCommands = new Collection<SqlCommand>();
            SqlCommand command;

            foreach (CompanyAsset companyAsset in companyAssets)
            {
                command = new SqlCommand("SP_MapAssetToCompanyAssets"); // Name of the SP
                command = ConstructCommand(command, companyAsset);
                sqlCommands.Add(command);
            }

            CommonData commonData = new CommonData();
            commonData.RunTransaction(sqlCommands);
        }

        public Collection<CompanyAsset> GetMappedAssetByOrganizationLevelListId(int organizationLevelListId)
        {
            SqlCommand command = new SqlCommand("SP_GetMappedAssetByOrganizationLevelListId"); // Name of the SP

            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.AddWithValue("@organizationLevelListId", organizationLevelListId);

            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);

            Collection<CompanyAsset> companyAssets = new Collection<CompanyAsset>();
            CompanyAsset companyAsset;

            while (dataReader.Read())
            {
                companyAsset = new CompanyAsset();
                companyAsset.CompanyAssetId = Convert.ToInt64(dataReader["CompanyAssetId"]);
                companyAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                companyAsset.Asset = new AssetData().GetByIdAsset(companyAsset.AssetId);
                companyAsset.IsAssetDepreciable = Convert.ToBoolean(dataReader["IsAssetDepreciable"]);
                companyAsset.LifeTime = Convert.ToInt64(dataReader["LifeTime"]);
                companyAsset.LifeTimeUnitOfMeasureId = Convert.ToInt64(dataReader["LifeTimeUnitOfMeasureId"]);
                //companyProductDomain.InventoryProductDom = new InventoryProductDomain { Description = dataReader["Description"].ToString() };
                //companyAsset.DepreciationMethodId = Convert.ToInt64(dataReader["DepreciationMethodId"]);
                companyAsset.DepreciationRate = Convert.ToDecimal(dataReader["DepreciationRate"]);
                companyAsset.ResidualValue = Convert.ToDecimal(dataReader["ResidualValue"]);
                companyAsset.AssetSerialFormat = Convert.ToString(dataReader["AssetSerialFormat"]);
                companyAsset.Barcode = Convert.ToString(dataReader["Barcode"]);

                companyAssets.Add(companyAsset);
            }
            dataReader.Close();
            return companyAssets;
        }

        public CompanyAsset GetCompanyAssetByAssetId(long assetId)
        {
            SqlCommand command = new SqlCommand("SP_GetCompanyAssetByAssetId");
            command.Parameters.AddWithValue("@AssetId", assetId);
            command.CommandType = CommandType.StoredProcedure;
            CommonData commonData = new CommonData(); SqlDataReader dataReader = commonData.ExecuteReader(command);
            CompanyAsset companyAsset = new CompanyAsset();
            while (dataReader.Read())
            {
                if (!(String.IsNullOrEmpty(dataReader["CompanyAssetId"].ToString())))
                {
                    companyAsset.CompanyAssetId = Convert.ToInt64(dataReader["CompanyAssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["OrganizationLevelListId"].ToString())))
                {
                    companyAsset.OrganizationLevelListId = Convert.ToInt64(dataReader["OrganizationLevelListId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetId"].ToString())))
                {
                    companyAsset.AssetId = Convert.ToInt64(dataReader["AssetId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["IsAssetDepreciable"].ToString())))
                {
                    companyAsset.IsAssetDepreciable = Convert.ToBoolean(dataReader["IsAssetDepreciable"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTime"].ToString())))
                {
                    companyAsset.LifeTime = Convert.ToInt32(dataReader["LifeTime"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["LifeTimeUnitOfMeasureId"].ToString())))
                {
                    companyAsset.LifeTimeUnitOfMeasureId = Convert.ToInt64(dataReader["LifeTimeUnitOfMeasureId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationMethodId"].ToString())))
                {
                    companyAsset.DepreciationMethodId = Convert.ToInt64(dataReader["DepreciationMethodId"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["DepreciationRate"].ToString())))
                {
                    companyAsset.DepreciationRate = Convert.ToDecimal(dataReader["DepreciationRate"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["ResidualValue"].ToString())))
                {
                    companyAsset.ResidualValue = Convert.ToDecimal(dataReader["ResidualValue"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["AssetSerialFormat"].ToString())))
                {
                    companyAsset.AssetSerialFormat = Convert.ToString(dataReader["AssetSerialFormat"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["Barcode"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["Barcode"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["sys_DateCreation"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["sys_DateCreation"]);
                }
                if (!(String.IsNullOrEmpty(dataReader["sys_DateLastModification"].ToString())))
                {
                    companyAsset.Barcode = Convert.ToString(dataReader["sys_DateLastModification"]);
                }
            }
            dataReader.Close();

            return companyAsset;
        }

        public SqlCommand ConstructCommand(SqlCommand command, CompanyAsset companyAssets)
        {
            command.CommandType = CommandType.StoredProcedure;

            try
            {
                command.Parameters.AddWithValue("@OrganizationLevelListId", companyAssets.OrganizationLevelListId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetId", companyAssets.AssetId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@IsAssetDepreciable", companyAssets.IsAssetDepreciable);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@LifeTime", companyAssets.LifeTime);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@LifeTimeUnitOfMeasureId", companyAssets.LifeTimeUnitOfMeasureId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@DepreciationMethodId", companyAssets.DepreciationMethodId);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@DepreciationRate", companyAssets.DepreciationRate);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@ResidualValue", companyAssets.ResidualValue);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@AssetSerialFormat", companyAssets.AssetSerialFormat);
            }
            catch (Exception)
            { }
            try
            {
                command.Parameters.AddWithValue("@Barcode", companyAssets.Barcode);
            }
            catch (Exception)
            { }
            //try
            //{
            //    command.Parameters.AddWithValue("@sys_DateCreation", companyAssets.SysDateCreation);
            //}
            //catch (Exception)
            //{ }
            //try
            //{
            //    command.Parameters.AddWithValue("sys_DateLastModification", companyAssets.SysDateLastModification);
            //}
            //catch (Exception)
            //{ }
            return command;
        }

    }
}

